package com.manage.controller;

import com.manage.pojo.Addresses;
import com.manage.pojo.operation.AddressesList;
import com.manage.service.AddressesService;
import com.manage.utils.FBJSONResult;
import com.manage.utils.PagedResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(value = "有关地址信息的操作", tags = {"有关地址信息的操作"})
public class AddressesController {

    @Autowired
    private AddressesService addressesService;

    @ApiOperation(value = "地址信息添加", notes = "地址信息添加接口")
    @PostMapping("/address/addressRegist")
    public FBJSONResult addressRegist(@RequestBody Addresses address) throws Exception {
        address.setUserId(address.getUserId());
        address.setAddressContent(address.getAddressContent());
        address.setAddressName(address.getAddressName());
        address.setAddressPhone(address.getAddressPhone());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        address.setCreateTime(sdf.format(new Date()));
        addressesService.saveAddresses(address);
        return FBJSONResult.ok(address);
    }

    @ApiOperation(value = "地址信息删除", notes = "地址信息删除接口")
    @ApiImplicitParam(name = "addressId", value = "地址id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/address/addressDelete")
    public FBJSONResult addressDelete(String addressId) throws Exception {
        if(StringUtils.isBlank(addressId)) {
            return FBJSONResult.errorMsg("地址id不能为空");
        }
        boolean addressIdIsExit = addressesService.queryAddressesIsExit(addressId);
        if(addressIdIsExit) {
            addressesService.addressDelete(addressId);
            return FBJSONResult.ok("删除成功");
        }else {
            return FBJSONResult.errorMsg("删除失败");
        }
    }

    @ApiOperation(value = "地址信息修改", notes = "地址信息修改接口")
    @PostMapping("/address/addressUpdate")
    public FBJSONResult addressUpdate(@RequestBody Addresses address) throws Exception {
        boolean addressIdIsExit = addressesService.queryAddressesIsExit(address.getAddressId());
        if(addressIdIsExit) {
            Addresses addressExample = new Addresses();
            addressExample.setAddressId(address.getAddressId());
            addressExample.setUserId(address.getUserId());
            addressExample.setAddressContent(address.getAddressContent());
            addressExample.setAddressName(address.getAddressName());
            addressExample.setAddressPhone(address.getAddressPhone());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            addressExample.setCreateTime(sdf.format(new Date()));
            addressesService.addressUpdate(addressExample);
            return FBJSONResult.ok(addressExample);
        } else {
            return FBJSONResult.errorMsg("更新失败");
        }

    }

    @ApiOperation(value = "地址信息查询(根据地址id)", notes = "地址信息查询接口(根据地址id)")
    @ApiImplicitParam(name = "addressId", value = "地址id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/address/queryAddressInfoByAddressId")
    public FBJSONResult queryAddressInfoByAddressId(String addressId) throws Exception {
        if(StringUtils.isBlank(addressId)) {
            return FBJSONResult.errorMsg("地址id不能为空");
        }
        AddressesList addressResult = addressesService.queryAddressesInfoByAddressId(addressId);
        return FBJSONResult.ok(addressResult);
    }

    @ApiOperation(value = "地址信息列表查询(根据用户id)", notes = "地址信息列表查询接口(根据用户id)")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/address/queryAddressesInfoByUserId")
    public FBJSONResult queryAddressesInfoByUserId(Integer page, String userId) throws Exception {
        if(StringUtils.isBlank(userId)) {
            return FBJSONResult.errorMsg("用户id不能为空");
        }
        if(page == null) {
            page = 1;
        }
        PagedResult result = addressesService.queryAddressesByUserId(page, 10, userId);
        return FBJSONResult.ok(result);
    }

}
